package com.zksk.admin.controller.order;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksk.admin.domain.orders.vo.OrdersQuestionRequestVo;
import com.zksk.admin.service.orders.OrdersQuestionService;
import com.zksk.common.core.domain.R;
import com.zksk.common.core.utils.StringUtils;
import com.zksk.common.core.web.controller.BaseController;
import com.zksk.common.log.annotation.Log;
import com.zksk.common.log.enums.BusinessType;
import com.zksk.mbg.admin.domain.OrdersQuestion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/order/question")
@Api(value = "OrderQuestionController", tags = "订单问题相关接口")
public class OrderQuestionController extends BaseController {

    @Autowired
    private OrdersQuestionService ordersQuestionService;

    /**
     * 获取订单问题列表
     * @param requestVo
     * @return
     */
    @Log(title = "获取订单问题列表 分页", businessType = BusinessType.SELECT)
    @ApiOperation(value = "获取订单问题列表")
    @PostMapping("/list")
    public R<IPage<OrdersQuestion>> page(@RequestBody OrdersQuestionRequestVo requestVo){

        Page<OrdersQuestion> page = ordersQuestionService.page(
                new Page<>(requestVo.getPageNum(), requestVo.getPageNum()),
                new LambdaQueryWrapper<OrdersQuestion>()
                        .like(StringUtils.isNotEmpty(requestVo.getOperatorName()), OrdersQuestion::getOperatorName, requestVo.getOperatorName())
        );

        return R.ok(page, "获取订单列表数据成功");
    }

    /**
     * 获取订单问题列表
     * @param ordersId
     * @return
     */
    @Log(title = "获取订单问题列表", businessType = BusinessType.SELECT)
    @ApiOperation(value = "获取订单问题列表")
    @GetMapping("/listByOrdersId")
    public R<List<OrdersQuestion>> list(@RequestParam("ordersId") Long ordersId){
        List<OrdersQuestion> list = ordersQuestionService.list(
                new LambdaQueryWrapper<OrdersQuestion>()
                        .eq(OrdersQuestion::getOrdersId, ordersId)
        );

        return R.ok(list, "获取订单列表数据成功");
    }


    /**
     * 查看订单问题
     * @param ordersQuestionId
     * @return
     */
    @Log(title = "查看订单问题", businessType = BusinessType.SELECT)
    @ApiOperation(value = "查看订单问题")
    @GetMapping("/{ordersQuestionId}")
    public R<OrdersQuestion> select(@PathVariable("ordersQuestionId") Long ordersQuestionId){

        OrdersQuestion byId = ordersQuestionService.getById(ordersQuestionId);

        return R.ok(byId, "查看订单问题详情数据成功");
    }


    @Log(title = "删除订单问题", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除订单问题(逻辑删除)")
    public R<String> remove(@PathVariable List<Long> ids) {
        ordersQuestionService.removeBatchByIds(ids);
        return R.ok("删除成功");
    }

    /**
     * 新增订单问题
     * @param ordersQuestion
     * @return
     */
    @Log(title = "订单问题管理", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增订单问题")
    public R<OrdersQuestion> add(@Validated(Insert.class) @RequestBody OrdersQuestion ordersQuestion) {
        ordersQuestionService.save(ordersQuestion);
        return R.ok(ordersQuestion,"新增成功");
    }

    /**
     * 更新订单问题
     * @param ordersQuestion
     * @return
     */
    @Log(title = "订单问题管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "更新问题")
    public R<OrdersQuestion> update(@Validated(Update.class) @RequestBody OrdersQuestion ordersQuestion) {
        ordersQuestionService.updateById(ordersQuestion);
        return R.ok(ordersQuestion,"更新成功");
    }

}
